import React, { FC, useRef } from 'react'
import { uploadFileTwoAPI } from '../api/api'

const UploadFile:FC<{
    children?:any,
    getUploadUrl?:any
}> = ({
    children,
    getUploadUrl,
}) => {

    const fileRef:any = useRef()
    const todoStartUpload = ()=>{
        fileRef.current.click()
    }

    // 上传文件
    const onChange = async() => {
        const file = fileRef.current.files[0]
        // console.log(file)
        const data = new FormData()  
        data.append("file",file)
        let res:any = await uploadFileTwoAPI(data)
        if(res.code == '200'){
            getUploadUrl(res.path)
        }
    }

  return (
    <div onClick={todoStartUpload}>
        <input type="file" style={{display:'none'}} ref={fileRef} onChange={onChange}/>
        {
            children
        }
    </div>
  )
}

export default UploadFile